package mysupertux.openGL;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import org.lwjgl.opengl.GL11;
import org.lwjgl.util.Rectangle;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.util.BufferedImageUtil;

public class SmallTexture {
	private Texture texture = null;

	/** Rectanlge (0, 0, width, height). */
	private Rectangle textureRectangle = null;
	
	public SmallTexture(String path) throws Exception {
		// the line that reads the image file
		BufferedImage image = ImageIO.read(new File(path));
		texture = BufferedImageUtil.getTexture(path, image);

	    textureRectangle = new Rectangle(0, 0, texture.getTextureWidth(), texture.getTextureHeight());
	}
	
	public int getWidth() {
		
		return texture.getTextureWidth();
	}
	
	public int getHeight() {
		return texture.getTextureHeight();
	}

	/**
	 * Draws the texture.
	 * 
	 * @param x
	 *            x
	 * @param y
	 *            y
	 */
	public void draw(int x, int y) {
		draw(x, y, textureRectangle, 1.0f, 1.0f, 0.0f);
	}

	/**
	 * Draws the texture.
	 * 
	 * @param x
	 *            x
	 * @param y
	 *            y
	 * @param scaleHorizontal
	 *            horizontal stretch factor
	 * @param scaleVertical
	 *            vertical stretch factor
	 * @param angle
	 *            angle (0..360)
	 */
	public void draw(int x, int y, float scaleHorizontal, float scaleVertical,
			float angle) {
		draw(x, y, textureRectangle, scaleHorizontal, scaleVertical, angle);
	}

	/**
	 * Draws the texture.
	 * 
	 * @param x
	 *            x
	 * @param y
	 *            y
	 * @param centerX
	 *            rotation center
	 * @param centerY
	 *            rotation center
	 * @param scaleHorizontal
	 *            horizontal stretch factor
	 * @param scaleVertical
	 *            vertical stretch factor
	 * @param angle
	 *            angle (0..360)
	 */
	public void draw(int x, int y, int centerX, int centerY,
			float scaleHorizontal, float scaleVertical, float angle) {
		draw(x, y, textureRectangle, centerX, centerY, scaleHorizontal,
				scaleVertical, angle);
	}

	/**
	 * Draws the texture.
	 * 
	 * @param x
	 *            x
	 * @param y
	 *            y
	 * @param part
	 *            the part of the image to draw
	 * @param scaleHorizontal
	 *            horizontal stretch factor
	 * @param scaleVertical
	 *            vertical stretch factor
	 * @param angle
	 *            angle (0..360)
	 */
	public void draw(int x, int y, Rectangle part, float scaleHorizontal,
			float scaleVertical, float angle) {
		int centerX = part.getWidth() / 2;
		int centerY = part.getHeight() / 2;

		draw(x, y, part, centerX, centerY, scaleHorizontal, scaleVertical,
				angle);
	}

	/**
	 * Draws the texture.
	 * 
	 * @param x
	 *            x
	 * @param y
	 *            y
	 * @param part
	 *            the part of the image to draw
	 * @param centerX
	 *            rotation center
	 * @param centerY
	 *            rotation center
	 * @param scaleHorizontal
	 *            horizontal stretch factor
	 * @param scaleVertical
	 *            vertical stretch factor
	 * @param angle
	 *            angle (0..360)
	 */
	public void draw(int x, int y, Rectangle part, int centerX, int centerY,
			float scaleHorizontal, float scaleVertical, float angle) {
		centerX += x;
		centerY += y;

		// rotation
		GL11.glMatrixMode(GL11.GL_MODELVIEW);

		GL11.glTranslatef(centerX, centerY, 0);
		GL11.glRotatef(angle, 0.0f, 0.0f, -1.0f);
		GL11.glScalef(scaleHorizontal, scaleVertical, 1.0f);
		GL11.glTranslatef(-centerX, -centerY, 0);

		// draw the texture
		float textureLeft = (float) part.getX() / texture.getTextureWidth();
		float textureRight = (float) (part.getX() + part.getWidth())
				/ texture.getTextureWidth();
		float textureTop = (float) part.getY() / texture.getTextureHeight();
		float textureBottom = (float) (part.getY() + part.getHeight())
				/ texture.getTextureHeight();

		texture.bind(); // GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
		GL11.glBegin(GL11.GL_QUADS);
		{
			GL11.glTexCoord2f(textureLeft, textureTop);
			GL11.glVertex2i(x, y);
			GL11.glTexCoord2f(textureRight, textureTop);
			GL11.glVertex2i(x + part.getWidth(), y);
			GL11.glTexCoord2f(textureRight, textureBottom);
			GL11.glVertex2i(x + part.getWidth(), y + part.getHeight());
			GL11.glTexCoord2f(textureLeft, textureBottom);
			GL11.glVertex2i(x, y + part.getHeight());
		}
		GL11.glEnd();

		GL11.glLoadIdentity();
	}

}
